package com.airport.schedule;

import com.airport.schedule.genetic.MiniGeneticScheduler;
import com.airport.schedule.genetic.RawGeneticScheduler;
import com.airport.schedule.model.Flight;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

public class Main {

    public static void main(String args[]) throws Exception {
        test1(args);
    }

    private static void rawScheduler(String args[]) throws Exception{
        int size = Integer.valueOf(args[0]);
        double m = Double.valueOf(args[1]);
        double c = Double.valueOf(args[2]);
        int iters = Integer.valueOf(args[3]);

        String dir = "/airportdata/";
        ProblemManager manager = new ProblemManager(dir+"data.xlsx");
        List<Problem> problemList = manager.genProblems(true);
        List<RawGeneticScheduler> schedulers = new ArrayList<>();
        for(Problem p : problemList) {
            RawGeneticScheduler scheduler = new RawGeneticScheduler(p, size, m, c, iters);
            schedulers.add(scheduler);
            scheduler.start();
        }
        for(RawGeneticScheduler scheduler : schedulers) {
            scheduler.join();
        }
        ScheduleSolution sol = null;
        for(RawGeneticScheduler s : schedulers) {
            if(sol == null) {
                sol = s.getSolution();
            }else {
                sol.merge(s.getSolution());
            }
        }
        ConflictHelper.removeConflictFlights(sol);
        System.out.println(ScheduleEvaluator.describe(sol,
                manager.getTotalFlights(), manager.getTotalPeople()));
        sol.dumpSchedule(dir + "genetic_"
                + ScheduleEvaluator.getScore(sol, manager.getTotalFlights(), manager.getTotalPeople())
                +".csv");

    }

    private static void test1(String args[]) throws Exception {
        String dir = "/opt/nlp/airportdata/";
        ProblemManager manager = new ProblemManager(dir+"data.xlsx");
        Problem problem = manager.genProblem();
        String[] files = new String[args.length-9];
        for(int i = 0; i < files.length; i++) {
            files[i] = dir+args[i+9];
        }
        MiniGeneticScheduler scheduler = new MiniGeneticScheduler(problem,
                Integer.valueOf(args[0]), Double.valueOf(args[1]), Double.valueOf(args[2]), Double.valueOf(args[3]),
                Integer.valueOf(args[4]),
                files,
                Double.valueOf(args[5]),
                Double.valueOf(args[6]),
                Double.valueOf(args[7]),
                Double.valueOf(args[8]));
        scheduler.run();
        ScheduleSolution sol = scheduler.getSolution();
        Set<Flight> rm = ConflictHelper.removeConflictFlights(sol);
        System.out.println("删除了 "+rm.size()+"只航班");
        System.out.println(ScheduleEvaluator.describe(sol,
                manager.getTotalFlights(), manager.getTotalPeople()));
        sol.dumpSchedule(dir + "genetic_"
                + ScheduleEvaluator.getScore(sol, manager.getTotalFlights(), manager.getTotalPeople())
                +".csv");
    }

}
